//
//  SegmentController.m
//  DianDangDai
//
//  Created by bhf on 2017/12/13.
//  Copyright © 2017年 bhf. All rights reserved.
//

#import "SegmentController.h"
#import "InvestmentProjectController.h"
#import "BondViewController.h"
@interface SegmentController ()
@property (nonatomic, strong) InvestmentProjectController *bmvc;
@property (nonatomic, strong) BondViewController *bond;
@property (nonatomic, strong) UIView *line;
@end

@implementation SegmentController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpChildViewController];
    [self setUpView];
    // Do any additional setup after loading the view.
}

- (void)setUpView {
    self.navigationItem.backBarButtonItem = BarButtonItem;
    self.title = @"我要投资";
    NSArray *segmentedArray = [NSArray arrayWithObjects:@"投资项目",@"债权转让",nil];
    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    
    segment.frame = CGRectMake(0, 64, SCREEN_WIDTH, 34);
    segment.selectedSegmentIndex = 0;
    segment.tintColor = [UIColor clearColor];
    [segment addTarget:self action:@selector(DidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
    segment.tintColor = [UIColor clearColor];//去掉颜色,现在整个segment都看不见
    NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName: [UIColor colorWithHex:@"#3399ff"]};
    [segment setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName: [UIColor colorWithHex:@"#666666"]};
    [segment setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:segment];
    
    _line  = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(segment.frame), SCREEN_WIDTH/2, 1)];
    _line.backgroundColor = [UIColor colorWithHex:@"#3399ff"];
    [self.view addSubview:_line];
    
    
}

//子控制器
- (void)setUpChildViewController {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Investment" bundle:[NSBundle mainBundle]];
    _bond = [storyboard instantiateViewControllerWithIdentifier:@"bond"];
    [self addChildViewController:_bond];
    [_bond didMoveToParentViewController:self];
    _bond.view.frame = CGRectMake(0, 100, SCREEN_WIDTH, SCREEN_HEIGHT-100);
    [self.view addSubview:_bond.view];
  
    _bmvc = [storyboard instantiateViewControllerWithIdentifier:@"bm"];
    [self addChildViewController:_bmvc];
    [_bmvc didMoveToParentViewController:self];
    _bmvc.view.frame = CGRectMake(0, 100, SCREEN_WIDTH, SCREEN_HEIGHT-100);
    [self.view addSubview:_bmvc.view];
    
}

- (void)DidChangeForSegmentedControl:(UISegmentedControl *)sender {
    NSInteger selecIndex = sender.selectedSegmentIndex;
    if (selecIndex == 0) {
        [self transitionFromViewController:_bond toViewController:_bmvc duration:0.2 options:UIViewAnimationOptionCurveEaseIn animations:^{
            _line.frame = CGRectMake(0, 99, SCREEN_WIDTH/2, 1);
        } completion:^(BOOL finished) {
            
        }];
        
    } else if (selecIndex == 1) {
        [self transitionFromViewController:_bmvc toViewController:_bond duration:0.2 options:UIViewAnimationOptionCurveEaseIn animations:^{
            _line.frame = CGRectMake(SCREEN_WIDTH/2, 99, SCREEN_WIDTH/2, 1);
        } completion:^(BOOL finished) {
            
        }];
        
    }
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
